import {create, createStore} from 'zustand';
import {immer} from 'zustand/middleware/immer'; // 简写方式中间件
import { devtools, persist,createJSONStorage } from 'zustand/middleware'; // 调试工具中间件, 持久化存储中间件

const useUserInfoStore = create(
    persist(
        immer((set, get) => ({
          price: 100,
          count: 1,
          color: 'red',
    
          addCount: () => {
            set({ count: get().count + 1 })
          },
    
          removeCount: () => {
            set(state => { state.count-- })
          },
    
          // 强制测试持久化
          forceSave: () => {
            set(state => {}) // 空操作触发保存
          }
        })),
        {
          name: 'myAppleStoreInfo',
          storage: createJSONStorage(() => localStorage),
          onRehydrateStorage: () => (state) => {
            console.log('持久化数据已加载', state)
          }
        }
      )
);



export default useUserInfoStore;




// const useUserInfoStore = create(
//     persist(
//             immer((set, get) => {
//                 return {
//                     count: 100
//                 }
//             }),
//             {name: 'userInfoStore11111',  storage: createJSONStorage(() => localStorage)}
//     )
// );

// {
//     //状态
//     name: '张三',
//     age:18,
//     sex:'男',
//     address:'北京',
//     phone:'13888888888',

//     //动态
//     setName:(name) => {
//         set(state => {state.name = name});
//     },
//     setAge:(age) => {
//         set(state => {state.age = age});
//     },
//     getPhone:() => {
//         return get().phone;
//     },

//     setPhone: async(phone) => {
//         console.log('ajax ...');
//         await new Promise(resolve => {
//             setTimeout(() => {
//                 set(state => {state.phone = phone});
//                 resolve();
//             }, 2000);
//         })
//     }
// }